import React from 'react';
import { fetchGradeTypeHistory } from '../service';

const useGradeTypeHistory = (id, type) => {
    const [outData, setOutData] = React.useState([]);
    const [loading, setLoading] = React.useState(true);
    const [outTotal, setOutTotal] = React.useState(0);

    const fetchData = async () => {
        setLoading(true);
        const { data = {}, code } = (await fetchGradeTypeHistory(id)) || {};
        const { total = 0, rows = [] } = data;

        if (code === 10000) {
            setOutData(rows);
            setOutTotal(total);
        }
        setLoading(false);
    };

    React.useEffect(() => {
        if (id) {
            fetchData();
        }
    }, [id, type]);

    return [loading, outData, outTotal];
};

export default useGradeTypeHistory;
